package dal

import (
	"context"

	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"

	"gorm.io/gorm"
)

// Get member storage instance
func GetMemberDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.Member))
}

// 小程序用户端
type Member struct {
	DB *gorm.DB
}

// Query members from the database based on the provided parameters and options.
func (a *Member) Query(ctx context.Context, params schema.MemberQueryParam, opts ...schema.MemberQueryOptions) (*schema.MemberQueryResult, error) {
	var opt schema.MemberQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}
	db := GetMemberDB(ctx, a.DB)
	if v := params.LikeNickname; len(v) > 0 {
		db = db.Where("a.nickname LIKE ?", "%"+v+"%")
	}
	var list schema.Members
	pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	queryResult := &schema.MemberQueryResult{
		PageResult: pageResult,
		Data:       list,
	}
	return queryResult, nil
}

// Get the specified member from the database.
func (a *Member) Get(ctx context.Context, id string, opts ...schema.MemberQueryOptions) (*schema.Member, error) {
	var opt schema.MemberQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.Member)
	ok, err := util.FindOne(ctx, GetMemberDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Get the specified member from the database.
func (a *Member) GetOpenid(ctx context.Context, openid string, opts ...schema.MemberQueryOptions) (*schema.Member, error) {
	var opt schema.MemberQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.Member)
	ok, err := util.FindOne(ctx, GetMemberDB(ctx, a.DB).Where("openid=?", openid), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Exist checks if the specified member exists in the database.
func (a *Member) Exists(ctx context.Context, id string) (bool, error) {
	ok, err := util.Exists(ctx, GetMemberDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

// Exist checks if the specified member exists in the database.
func (a *Member) ExistsOpenid(ctx context.Context, openid string) (bool, error) {
	ok, err := util.Exists(ctx, GetMemberDB(ctx, a.DB).Where("openid=?", openid))
	return ok, errors.WithStack(err)
}

// Create a new member.
func (a *Member) Create(ctx context.Context, item *schema.Member) error {
	result := GetMemberDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

// Update the specified member in the database.
func (a *Member) Update(ctx context.Context, item *schema.Member) error {
	result := GetMemberDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
	return errors.WithStack(result.Error)
}

// Delete the specified member from the database.
func (a *Member) Delete(ctx context.Context, id string) error {
	result := GetMemberDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Member))
	return errors.WithStack(result.Error)
}

// 更新 手机号
func (a *Member) UpdatePhoneByID(ctx context.Context, id string, phone string) error {
	result := GetMemberDB(ctx, a.DB).Where("id=?", id).Select("phone").Updates(schema.Member{Phone: phone})
	return errors.WithStack(result.Error)
}

// 更新 头像
func (a *Member) UpdateNicknameAvatarByID(ctx context.Context, id string, avatar, nickname string) error {
	result := GetMemberDB(ctx, a.DB).Where("id=?", id).
		Select("avatar", "nickname").
		Updates(schema.Member{Avatar: avatar, Nickname: nickname})
	return errors.WithStack(result.Error)
}
